package com.fhz.example.handler;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.MessageQueue;
import android.util.Log;
import android.view.View;

import com.fhz.example.R;

public class HandlerActivity extends AppCompatActivity {

    private static final String TAG = "feng";

    /**
     *  内存泄漏解决
     *  https://blog.csdn.net/javazejian/article/details/50839443
     *
     *  常见的各种内存泄漏几解决方案
     *  https://www.jianshu.com/p/f0e47bcb1866
     */

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what == 100){
                Log.i(TAG, "handleMessage: "+(String) msg.obj);
            }else if(msg.what == 200){
                Log.i(TAG, "handleMessage: "+(String) msg.obj);
            }
        }
    };

    private Handler handler2 = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            Log.i(TAG, "handleMessage:22的消息 "+msg.what);
        }
    };


    /**
     *  https://blog.csdn.net/weixin_34235105/article/details/91363436
     *  一个线程可以有几个Looper？几个Handler？从Looper.prepare()来看看关于Looper的一些问题
     */

    //子线程handler
    private Handler subHandler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler);


        /**
         * handlerThread的原理
         * https://blog.csdn.net/javazejian/article/details/52426353
         */



        Message obtain = Message.obtain();
        obtain.what = 100;
        obtain.obj = "主线程发送的111";
        handler.sendMessage(obtain);



        new Thread(new Runnable() {
            @Override
            public void run() {
                Message obtain = Message.obtain();
                obtain.what = 200;
                obtain.obj = "子线程发送22222";
                handler.sendMessage(obtain);
            }
        }).start();



        new Thread(new Runnable() {
            @Override
            public void run() {
                Message obtain = Message.obtain();
                obtain.what = 200;
                obtain.obj = "另外一个handler";
                handler2.sendMessage(obtain);
            }
        }).start();



        //子线程创建
        new Thread(new Runnable() {
            @Override
            public void run() {
                //准备
                Looper.prepare();//new Looper(new MessageQueue())

                subHandler = new Handler(){
                    @Override
                    public void handleMessage(@NonNull Message msg) {
                        super.handleMessage(msg);
                        if(msg.what == 300){
                            Log.i(TAG, "handleMessage: "+(String) msg.obj);
                        }
                    }
                };


                //Android中为什么主线程不会因为Looper.loop()里的死循环卡死？
                //https://www.zhihu.com/question/34652589
                //开始抽取
                Looper.loop();

            }
        }).start();




    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }

    public void send(View view) {
        Message obtain = Message.obtain();
        obtain.what = 300;
        obtain.obj = "我是主线程发过来的";
        subHandler.sendMessage(obtain);
    }
}